package com.crm.service.saling.impl;

import com.crm.dao.saling.ContractDao;
import com.crm.dao.saling.DispatcherDao;
import com.crm.dao.saling.DispatcherDetailsDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.Contract;
import com.crm.pojo.Dispatcher;
import com.crm.pojo.DispatcherDetail;
import com.crm.service.saling.DispatcherDetailService;
import com.crm.vo.DispatcherContractClientProductVO;
import com.crm.vo.DispatcherDetailProductCODetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DispatcherDetailServiceImpl implements DispatcherDetailService {
    @Autowired
    private DispatcherDetailsDao dispatcherDetailsDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private DispatcherDao dispatcherDao;

    @Override
    public Long getDispatcherDetailsCount() {
        return dispatcherDetailsDao.getDispatcherDetailsCount();
    }

    @Override
    public List<DispatcherDetail> getDispatcherDetailListByDispatcherId(Long dispatcherId) {
        Dispatcher dispatcher = dispatcherDao.getDispatcherByDispatcherId(dispatcherId);
        List<DispatcherDetail> list = dispatcherDetailsDao.getDispatcherDetailListByDispatcherId(dispatcher);
        return list;
    }

    @Override
    public List<DispatcherContractClientProductVO> getDispatcherContractClientProductVOList(int currpage, int rows) throws ServiceException {
        try {
            PageRequest pageRequest = new PageRequest(currpage - 1, rows);
            return dispatcherDetailsDao.getDispatcherContractClientProductVOList(pageRequest);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public DispatcherDetail saveDispatcherDetail(DispatcherDetail dispatcherDetail) {
        try {
            return dispatcherDetailsDao.save(dispatcherDetail);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public List<DispatcherDetailProductCODetailVO> getDispatcherDetailList(Long contractId) throws ServiceException {

        try {
            Contract contract = contractDao.findOne(contractId);
            return dispatcherDetailsDao.getDispatcherDetailListByContractId(new BigDecimal(contractId.toString()), contract);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public List<DispatcherDetail> saveDispatcherDetailList(List<DispatcherDetail> list) throws ServiceException {

        try {
            Iterable iterable = dispatcherDetailsDao.save(list);
            List<DispatcherDetail> dispatcherDetailList = new ArrayList<>();
            iterable.forEach(e -> dispatcherDetailList.add((DispatcherDetail) e));
            return dispatcherDetailList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }

    }

    //根据发货单id查询所有发货单明细并且审核通过
    @Override
    public void toCheck(Long did) {
        List<DispatcherDetail> dd = dispatcherDetailsDao.findAllBydid(did);
        for (DispatcherDetail d : dd) {
            d.setFkDispatcherId(new BigDecimal(1));//审核通过
            dispatcherDetailsDao.save(d);
        }
    }
    @Override
    public List<DispatcherDetail> findAllBYdid(Long did){
        return dispatcherDetailsDao.findAllBydid(did);
    }
}
